n = int(input())
l1 = []
l2 = []
l3 = []
for i in range(n):
    t = input().split(' ')
    if t[1] == 'A':
        l1.append(t[0])
    if t[1] == 'B':
        l2.append(t[0])
    if t[1] == 'C':
        l3.append(t[0])
for i in l1:
    print(i)
for i in l2:
    print(i)
for i in l3:
    print(i)